package ${package}<#if moduleName != "">.${moduleName}<#else></#if>.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package}<#if moduleName != "">.${moduleName}<#else></#if>.utils.R;
import ${package}<#if moduleName != "">.${moduleName}<#else></#if>.entity.${className};
import ${package}<#if moduleName != "">.${moduleName}<#else></#if>.service.${className}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Arrays;

/**
* 用于操作数据库 ${tableName} 及其其他业务
*/
@CrossOrigin
@RestController
@RequestMapping("<#if moduleName != "">/${moduleName}</#if>/${className?uncap_first}")
public class ${className}Controller {
    @Autowired
    private ${className}Service ${className?uncap_first}Service;

    /**
    * 列表
    */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        Page<${className}> pages = new Page<>(0,10);

        if (params.get("currentPage") != null) {
            pages.setCurrent(Long.parseLong(params.get("currentPage").toString()));
        }else {
            pages.setCurrent(0);
        }

        if (params.get("pageSize") != null) {
            pages.setSize(Long.parseLong(params.get("pageSize").toString()));
        }else {
            pages.setSize(10);
        }

        QueryWrapper<${className}> wrapper = new QueryWrapper<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String k = entry.getKey();
            Object v = entry.getValue();
            if (k.equals("currentPage") || k.equals("pageSize")) {
                continue;
            }
            if (k.equals("key")) {
                // 查询参数的逻辑
                if ("".equals(v)) {

                }else {
                    // 查询主键
                    wrapper.eq("${pk.attrName}",v);
                }
                continue;
            }
            wrapper.eq(k,v);
        }
        ${className?uncap_first}Service.page(pages, wrapper);
        return R.ok().put("data",pages);
    }


    /**
    * 根据主键信息
    */
    @RequestMapping("/info/{id}")
    public R getInfo(@PathVariable("id") ${pk.attrType} ${pk.attrName}){
        ${className} ${className?uncap_first} = ${className?uncap_first}Service.getById(${pk.attrName});
        return R.ok().put("data",${className?uncap_first});
    }

    /**
    * 保存
    */
    @RequestMapping("/save")
    public R save(@RequestBody ${className} ${className?uncap_first}){
        ${className?uncap_first}Service.save(${className?uncap_first});
        return R.ok();
    }

    /**
    * 根据主键修改
    */
    @RequestMapping("/update")
    public R update(@RequestBody ${className} ${className?uncap_first}){
        ${className?uncap_first}Service.updateById(${className?uncap_first});
        return R.ok();
    }

    /**
    * 删除
    */
    @RequestMapping("/delete/{id}")
    public R delete(@PathVariable("id") ${pk.attrType} ${pk.attrName}){
        ${className?uncap_first}Service.removeById(${pk.attrName});
        return R.ok();
    }

    /**
    * 删除多个
    */
    @RequestMapping("/delete")
    public R deleteAll(@RequestBody ${pk.attrType}[] ${pk.attrName}s){
        ${className?uncap_first}Service.removeByIds(Arrays.asList(${pk.attrName}s));
        return R.ok();
    }
}
